﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.admin.weixinuser
{
    public partial class userinfo_list : MxWeiXinPF.Web.UI.ManagePage
    {
        protected int group_id;
        protected string keywords = string.Empty;
        protected int page;
        protected int pageSize;
        protected int totalCount;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            base.ChkAdminLevel("userinfo_list", MXEnums.ActionEnum.Delete.ToString());
            int num = 0;
            int num2 = 0;
            MxWeiXinPF.BLL.users users = new MxWeiXinPF.BLL.users();
            for (int i = 0; i < this.rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)this.rptList.Items[i].FindControl("hidId")).Value);
                CheckBox box = (CheckBox)this.rptList.Items[i].FindControl("chkId");
                if (box.Checked)
                {
                    if (users.Delete(id))
                    {
                        num++;
                    }
                    else
                    {
                        num2++;
                    }
                }
            }
            base.AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), string.Concat(new object[] { "删除用户", num, "条，失败", num2, "条" }));
            base.JscriptMsg(string.Concat(new object[] { "删除成功", num, "条，失败", num2, "条！" }), Utils.CombUrlTxt("userinfo_list.aspx", "group_id={0}&keywords={1}", new string[] { this.group_id.ToString(), this.keywords }), "Success");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("userinfo_list.aspx", "group_id={0}&keywords={1}", new string[] { this.group_id.ToString(), this.txtKeywords.Text }));
        }

        protected string CombSqlTxt(int _group_id, string _keywords)
        {
            StringBuilder builder = new StringBuilder();
            if (_group_id > 0)
            {
                builder.Append(" and groupid=" + _group_id);
            }
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                builder.Append(" and (nickname like '%" + _keywords + "%')");
            }
            return builder.ToString();
        }

        protected void ddlGroupId_SelectedIndexChanged(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("userinfo_list.aspx", "group_id={0}&keywords={1}", new string[] { this.ddlGroupId.SelectedValue, this.keywords }));
        }

        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("userinfo_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.group_id = MXRequest.GetQueryInt("group_id");
            this.keywords = MXRequest.GetQueryString("keywords");
            if (!this.Page.IsPostBack)
            {
                MxWeiXinPF.Model.manager adminInfo = base.GetAdminInfo();
                this.pageSize = this.GetPageSize(10);
                if (!this.Page.IsPostBack)
                {
                    this.TreeBind();
                    this.RptBind("id>0" + this.CombSqlTxt(this.group_id, this.keywords), "subscribe_time desc,id desc");
                }
            }
        }

        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = MXRequest.GetQueryInt("page", 1);
            if (this.group_id > 0)
            {
                this.ddlGroupId.SelectedValue = this.group_id.ToString();
            }
            this.txtKeywords.Text = this.keywords;
            this.rptList.DataSource = new MxWeiXinPF.BLL.users().GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();
            this.txtPageNum.Text = this.pageSize.ToString();
            string linkUrl = Utils.CombUrlTxt("userinfo_list.aspx", "group_id={0}&keywords={1}&page={2}", new string[] { this.group_id.ToString(), this.keywords, "__id__" });
            this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
        }

        private void TreeBind()
        {
            MxWeiXinPF.BLL.user_groups _groups = new MxWeiXinPF.BLL.user_groups();
            DataTable table = _groups.GetList(0, "", "id desc").Tables[0];
            this.ddlGroupId.Items.Clear();
            this.ddlGroupId.Items.Add(new ListItem("所有会员组", ""));
            foreach (DataRow row in table.Rows)
            {
                this.ddlGroupId.Items.Add(new ListItem(row["title"].ToString(), row["id"].ToString()));
            }
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
            {
                Utils.WriteCookie("userinfo_list_page_size", num.ToString(), 0x3840);
            }
            base.Response.Redirect(Utils.CombUrlTxt("userinfo_list.aspx", "group_id={0}&keywords={1}", new string[] { this.group_id.ToString(), this.keywords }));
        }

        public string GetTitle(string groupid)
        {
            return new MxWeiXinPF.BLL.wx_userinfo_BaseData().GetTitle(Convert.ToInt32(groupid));
        }
    }
}